package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;

@Cmd(code = GameProtocol.S_CROSS_STRONGHOLD_ELIMINATE_UNION_STATUS, desc = "")
public class StrongholdEliminateUnionSyncCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdEliminateUnionStatusRespMsg msg = StrongholdWarProto.CrossStrongholdEliminateUnionStatusRespMsg.parseFrom(packet.getBytes());
        long userId = msg.getUserId();

        if (userId > 0) {
            sendToPlayer(userId, msg.getExist());
        } else {
            String unionId = msg.getUnionId();
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionId);
            if (memberMap != null) {
                for (Long memberId : memberMap.keySet()) {
                    sendToPlayer(memberId, msg.getExist());
                }
            }
        }
    }

    private void sendToPlayer(long userId, boolean exist) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {
            StrongholdWarProto.CrossStrongholdEliminateUnionStatusSyncMsg.Builder syncMsg = StrongholdWarProto.CrossStrongholdEliminateUnionStatusSyncMsg.newBuilder();
            syncMsg.setExist(exist);
            player.sendPacket(ClientProtocol.U_STRONGHOLD_ELIMINATE_UNION_STATUS_SYNC, syncMsg);
        }
    }
}
